# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import json
import os.path
import time

from itemadapter import ItemAdapter


class OpinionspiderPipeline:

    def __init__(self):
        self.file_name = None
        self.folder = None
        self.today = None
        self.path = "C:/0/project/POMSv2/data"

    def open_spider(self, spider):
        self.today = time.strftime("%m%d", time.localtime())
        self.file_name = time.strftime("%H%M", time.localtime())
        self.folder = self.path + "/" + self.today
        if not os.path.exists(self.folder):
            os.mkdir(self.folder)

    def process_item(self, item, spider):
        with open(self.folder + "/" + self.file_name + ".log", "w+") as f:
            f.write(json.dumps(ItemAdapter(item).asdict()))
